package com.maket.jbxx.controller;

import java.security.Security;
import java.util.*;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.maket.jbxx.domain.vo.TreeSelect;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.CodeClient;
import com.ruoyi.system.api.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.maket.jbxx.domain.GcFactoryUnitTable;
import com.maket.jbxx.service.IGcFactoryUnitTableService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 工厂定义Controller
 * 
 * @author ruoyi
 * @date 2024-06-25
 */
@RestController
@RequestMapping("/gcdy")
public class GcFactoryUnitTableController extends BaseController
{
    @Autowired
    private IGcFactoryUnitTableService gcFactoryUnitTableService;

    @Resource
    private CodeClient codeClient;

    /**
     * 查询工厂定义列表
     */
    @RequiresPermissions("jbxx:gcdy:list")
    @GetMapping("/list")
    public TableDataInfo list(GcFactoryUnitTable gcFactoryUnitTable)
    {
        startPage();
        List<GcFactoryUnitTable> list = gcFactoryUnitTableService.selectGcFactoryUnitTableList(gcFactoryUnitTable);

        return getDataTable(list);
    }

    @RequiresPermissions("jbxx:gcdy:list")
    @GetMapping("/byList")
    public TableDataInfo byList(GcFactoryUnitTable gcFactoryUnitTable)
    {
        startPage();
        List<GcFactoryUnitTable> list = gcFactoryUnitTableService.selectGcFactoryUnitTableByList(gcFactoryUnitTable);

        return getDataTable(list);
    }


    /**
     * 导出工厂定义列表
     */
    @RequiresPermissions("jbxx:gcdy:export")
    @Log(title = "工厂定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GcFactoryUnitTable gcFactoryUnitTable)
    {
        List<GcFactoryUnitTable> list = gcFactoryUnitTableService.selectGcFactoryUnitTableList(gcFactoryUnitTable);
        ExcelUtil<GcFactoryUnitTable> util = new ExcelUtil<GcFactoryUnitTable>(GcFactoryUnitTable.class);
        util.exportExcel(response, list, "工厂定义数据");
    }

    /**
     * 获取工厂定义详细信息
     */
    @RequiresPermissions("jbxx:gcdy:query")
    @GetMapping(value = "/{dyCode}")
    public AjaxResult getInfo(@PathVariable("dyCode") String dyCode)
    {
        return success(gcFactoryUnitTableService.selectGcFactoryUnitTableByDyCode(dyCode));
    }

    /**
     * 新增工厂定义
     */
    @RequiresPermissions("jbxx:gcdy:add")
    @Log(title = "工厂定义", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GcFactoryUnitTable gcFactoryUnitTable)
    {
        gcFactoryUnitTable.setDyCode(codeClient.getCode(15));
        gcFactoryUnitTable.setDyCreate(SecurityUtils.getUsername());

        List<GcFactoryUnitTable> list = gcFactoryUnitTableService.selectGcFactoryUnitTableList(new GcFactoryUnitTable());

        for (int i = 0; i < list.size(); i++) {
            if (gcFactoryUnitTable.getDyParentCode().equals(list.get(i).getDyName())) {
                gcFactoryUnitTable.setDyParentCode(list.get(i).getDyCode());
            }
        }

        return toAjax(gcFactoryUnitTableService.insertGcFactoryUnitTable(gcFactoryUnitTable));
    }

    /**
     * 修改工厂定义
     */
    @RequiresPermissions("jbxx:gcdy:edit")
    @Log(title = "工厂定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GcFactoryUnitTable gcFactoryUnitTable)
    {

        gcFactoryUnitTable.setDyCreate(SecurityUtils.getUsername());

        gcFactoryUnitTable.setDyParentCode(gcFactoryUnitTable.getDy_ParentCode());

        System.out.println(gcFactoryUnitTable);

        return toAjax(gcFactoryUnitTableService.updateGcFactoryUnitTable(gcFactoryUnitTable));
    }

    /**
     *修改工厂定义的启禁用
     */
    @RequiresPermissions("jbxx:gcdy:edit")
    @Log(title = "工厂定义", businessType = BusinessType.UPDATE)
    @PutMapping("/isStart")
    public  AjaxResult updateGcFactoryUnitTableIsStart(@RequestBody GcFactoryUnitTable gcFactoryUnitTable){

        return toAjax(gcFactoryUnitTableService.updateGcFactoryUnitTableIsStart(gcFactoryUnitTable));
    }

    /**
     * 删除工厂定义
     */
    @RequiresPermissions("jbxx:gcdy:remove")
    @Log(title = "工厂定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dyCodes}")
    public AjaxResult remove(@PathVariable String[] dyCodes)
    {
        List<GcFactoryUnitTable> resultList = new ArrayList<>();

        // 遍历传入的编码数组，查询所有相关记录
        for (String dyCode : dyCodes) {
            GcFactoryUnitTable gcFactoryUnitTable = new GcFactoryUnitTable();
            gcFactoryUnitTable.setDyCode(dyCode);
            List<GcFactoryUnitTable> list = gcFactoryUnitTableService.selectGcFactoryUnitTableList(gcFactoryUnitTable);

            // 将查询结果添加到结果列表中
            resultList.addAll(list);
        }

        // 提取所有查询结果的编码，用于批量删除
        Set<String> codesToDelete = new HashSet<>();
        for (GcFactoryUnitTable table : resultList) {
            codesToDelete.add(table.getDyCode());
        }

        return toAjax( gcFactoryUnitTableService.deleteGcFactoryUnitTableByDyCodes(codesToDelete.toArray(new String[0])));
    }


    /**
     * 获取部门树列表
     */
    @RequiresPermissions("jbxx:gcdy:list")
    @GetMapping("/deptTree")
    public AjaxResult deptTree(GcFactoryUnitTable dept)
    {
        return success(gcFactoryUnitTableService.selectDeptTreeList(dept));
    }
}
